package cn.com.surker.material.service.impl;

import cn.com.surker.database.JDBCTemplateCache;
import cn.com.surker.enums.StateEnum;
import cn.com.surker.material.dao.MainMaterialInspectionDao;
import cn.com.surker.material.entity.MainMaterialInspection;
import cn.com.surker.material.service.MainMaterialInspectionService;
import cn.com.surker.system.entity.DataSourceInfo;
import cn.com.surker.util.StringUtils;
import cn.com.surker.weight.entity.WeightOrder;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Date;
import java.util.List;

/**
 * 抽奖基础类
 *
 * @author luotao
 * @date 2024/11/13
 */
public abstract class MainMaterialInspectionBaseImpl extends ServiceImpl<MainMaterialInspectionDao, MainMaterialInspection> implements MainMaterialInspectionService {

	@Override
	public MainMaterialInspection queryData(DataSourceInfo ds, String uCode) {
		if (ds == null || StringUtils.isBlank(uCode)) {
			return null;
		}
		JdbcTemplate localJdbcTemplate = JDBCTemplateCache.getLocalJdbcTemplate(ds);
		if (localJdbcTemplate == null) {
			return null;
		}
		//	id, org_id, code, weight_code, material_id, amount, sum_weight, check_date, is_print, check_status, status, remark, creator, create_time, updator, update_time
		String querySql = "select id, org_id, code, weight_code, material_id, amount, sum_weight, check_date, is_print, check_status, status, remark, creator, create_time, updator, update_time\n" +
			"from main_material_inspection where status = 0 and weight_code = '" + uCode + "'";
		List<MainMaterialInspection> weightSpotChecks = localJdbcTemplate.query(querySql, BeanPropertyRowMapper.newInstance(MainMaterialInspection.class));
		if (weightSpotChecks != null || weightSpotChecks.isEmpty()) {
			return null;
		}
		MainMaterialInspection inspection = weightSpotChecks.get(0);
		return inspection;
	}

	@Override
	public Integer updateCheck(WeightOrder weightOrder, DataSourceInfo ds, Long originId, String weightCode, Integer checkResult, String userName, String remarks) {
		if (ds == null || StringUtils.isBlank(weightCode) || checkResult == null || weightOrder == null) {
			return -1;
		}
		MainMaterialInspection inspection = queryData(ds, weightCode);
		JdbcTemplate localJdbcTemplate = JDBCTemplateCache.getLocalJdbcTemplate(ds);
		if (localJdbcTemplate == null) {
			return -1;
		}
		if (inspection == null) {
			//	没有查询处抽检数据
			Long supplierId = weightOrder.getSupplierId();
			String supplier = weightOrder.getSupplier();
			Long materialId = weightOrder.getMaterialId();
			String norms = weightOrder.getMaterialNorms();
			String busNumber = weightOrder.getBusNumber();
			String driver = weightOrder.getDriver();
			String updateSql = "INSERT INTO main_material_inspection(org_id,code,weight_code,material_id,amount,sum_weight,check_date,is_print,check_status,status,remark,creator,create_time)\n" +
				"     VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)";
			Date nowDate = new Date();
			Object[] objs = {null, null, weightCode, materialId, null, null, nowDate, 0, 0, StateEnum.ENABLED.getCode(), remarks, userName, nowDate};
			int update = localJdbcTemplate.update(updateSql, objs);
			return update;
		} else {
			String updateSql = "update main_material_inspection set check_status = ?,creator = ? where weight_code  = ?";
			Object[] objs = {checkResult, userName, weightCode};
			int update = localJdbcTemplate.update(updateSql, objs);
			return update;
		}
	}

	@Override
	public Boolean needCheck(DataSourceInfo ds, String code) {
		return false;
	}
}
